package com.blqy.fr.template.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blqy.fr.common.annotation.BlQyLogin;
import com.blqy.fr.common.result.Result;
import com.blqy.fr.model.template.Template;
import com.blqy.fr.model.template.TemplateTagRelation;
import com.blqy.fr.template.service.TemplateInfoService;
import com.blqy.fr.template.service.TemplateTagRelationService;
import com.blqy.fr.template.service.TemplateTagService;
import com.blqy.fr.vo.template.TemplateResponseVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "模板管理")
@RestController
@RequestMapping("api/template/templateInfo")
public class TemplateInfoApiController {

    @Autowired
    private TemplateInfoService templateInfoService;
    @Autowired
    private TemplateTagService templateTagService;
    @Autowired
    private TemplateTagRelationService templateTagRelationService;


    /**
     * 获取模板列表
     *
     * @return
     */
    @GetMapping("getTemplateList")
    public Result<List<Template>> getTemplateList() {
        List<Template> templateList = templateInfoService.list();
        return Result.ok(templateList);
    }

    /**
     * 获取模板信息
     *
     * @return
     */
    @GetMapping("getTemplateInfo/{templateName}")
    public Result<TemplateResponseVo> getTemplateInfo(@PathVariable String templateName) {
        TemplateResponseVo templateVoList = templateInfoService.getTemplateStructure(templateName);
        return Result.ok(templateVoList);
    }

    /**
     * 更新模板信息
     *
     * @return
     */
    @PutMapping("/structure")
    public Result updateTemplateStructure(@RequestBody @Valid TemplateResponseVo request) {
        templateInfoService.updateTemplateStructure(request);
        return Result.ok();
    }

    /**
     * 删除模板标签
     *
     * @return
     */
    @DeleteMapping("/deleteTag/{templateTagId}")
    public Result deleteTemplateTag(@PathVariable Long templateTagId) {
        templateTagService.removeById(templateTagId);
        templateTagRelationService.remove(new LambdaQueryWrapper<TemplateTagRelation>()
                .eq(TemplateTagRelation::getTemplateTagId, templateTagId));
        return Result.ok();
    }


    /*
        日志模块远程调用模板模块获取当前团队使用的模板name
     */
    @BlQyLogin
    @GetMapping("/getTemplateNameByTeamId/{teamId}")
    public Result<String> getTemplateNameByTeamId(@PathVariable Long teamId) {
        String templateName = templateInfoService.getTemplateNameByTeamId(teamId);
        return Result.ok(templateName);
    }


    /*
        日志模块远程调用模板模块获取当前团队使用的模板id
     */
    @BlQyLogin
    @GetMapping("/getTemplateIdByTeamId/{teamId}")
    public Result<Long> getTemplateIdByTeamId(@PathVariable Long teamId) {
        Long templateId = templateInfoService.getTemplateIdByTeamId(teamId);
        return Result.ok(templateId);
    }

}
